/*
* INSANE - Interactive Structural Analysis Environment
*
* Copyright (C) 2003-2005
* Universidade Federal de Minas Gerais
* Escola de Engenharia
* Departamento de Engenharia de Estruturas
* 
* Author's email :    insane@dees.ufmg.br
* Author's website :  http://www.dees.ufmg.br/insane
* 
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or any later version.
* 
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU General Public License for more details.
* 
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/
package br.ufmg.dees.insane.ui.rich.full.dialogs;

import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Locale;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JPanel;

import br.ufmg.dees.insane.commons.dialogs.SimpleDialog;
import br.ufmg.dees.insane.model.disc.node.Node;
import br.ufmg.dees.insane.ui.rich.full.InsaneInternalInterface;

/**
 * An abstract class extending JDialog representing a dialog on screen.
 *
 * @author Fonseca, Flavio & Pitangueira, Roque
 * @see gui.InsaneInternalInterface
 * @since April 2004
 */
public class NodeRestraintsDialog extends SimpleDialog {
	
	/**
	 * Comment for <code>serialVersionUID</code>
	 */
	private static final long serialVersionUID = 1L;
	private InsaneInternalInterface parent;
	private NumberFormat numberFormat;
	private String gAnlType;
	
	private JCheckBox restraintRdx = new JCheckBox("");
	private JCheckBox restraintRdy = new JCheckBox("");
	private JCheckBox restraintRdz = new JCheckBox("");
	private JCheckBox restraintRrx = new JCheckBox("");
	private JCheckBox restraintRry = new JCheckBox("");
	private JCheckBox restraintRrz = new JCheckBox("");
	public boolean Rdx;
	public boolean Rdy;
	public boolean Rdz;
	public boolean Rrx;
	public boolean Rry;
	public boolean Rrz;
	private JLabel labelRdx = new JLabel("Dx       ");
	private JLabel labelRdy = new JLabel("Dy       ");
	private JLabel labelRdz = new JLabel("Dz       ");
	private JLabel labelRrx = new JLabel("Rx       ");
	private JLabel labelRry = new JLabel("Ry       ");
	private JLabel labelRrz = new JLabel("Rz       ");
	private JLabel figuraRdx;
	private JLabel figuraRdy;
	private JLabel figuraRdz;
	private JLabel figuraRrx;
	private JLabel figuraRry;
	private JLabel figuraRrz;
	
//*****************************************************************************
	
	public NodeRestraintsDialog(InsaneInternalInterface parent) {
		this.parent = parent;
		gAnlType = parent.getModel().getDiscreteModel().getGlobalAnalysisModel().getType();
		
		setUpFormat();
		setUpFields();
		
		JPanel restraintPane = new JPanel();
		restraintPane.setPreferredSize(new Dimension(360, 280));
		restraintPane.setLayout(new GridLayout(4,1,2,2));
		JPanel restraintDPane = new JPanel();
		restraintDPane.setLayout(new GridLayout(1,3,2,2));
		restraintPane.add(restraintDPane);
		JPanel restraintValueDPane = new JPanel();
		restraintValueDPane.setLayout(new GridLayout(1,3,2,2));
		restraintPane.add(restraintValueDPane);
		JPanel restraintRPane = new JPanel();
		restraintRPane.setLayout(new GridLayout(1,3,2,2));
		restraintPane.add(restraintRPane);
		JPanel restraintValueRPane = new JPanel();
		restraintValueRPane.setLayout(new GridLayout(1,3,2,2));
		restraintPane.add(restraintValueRPane);
		
		figuraRdx = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDx64.gif")) ); 
		restraintDPane.add(figuraRdx);
		figuraRdy = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDy64.gif")) );
		restraintDPane.add(figuraRdy);
		figuraRdz = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDz64.gif")) );
		restraintDPane.add(figuraRdz);
		figuraRrx = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRx64.gif")) );
		restraintRPane.add(figuraRrx);
		figuraRry = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRy64.gif")) );
		restraintRPane.add(figuraRry);
		figuraRrz = new JLabel( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRz64.gif")) );
		restraintRPane.add(figuraRrz);
		
		JPanel restraintRdxPane = new JPanel();
		JPanel restraintRdyPane = new JPanel();
		JPanel restraintRdzPane = new JPanel();
		JPanel restraintRrxPane = new JPanel();
		JPanel restraintRryPane = new JPanel();
		JPanel restraintRrzPane = new JPanel();
		
		// register events for JCheckBox
		CheckBoxHandlerRdx handlerRdx = new CheckBoxHandlerRdx();
		restraintRdx.addItemListener( handlerRdx );
		CheckBoxHandlerRdy handlerRdy = new CheckBoxHandlerRdy();
		restraintRdy.addItemListener( handlerRdy );
		CheckBoxHandlerRdz handlerRdz = new CheckBoxHandlerRdz();
		restraintRdz.addItemListener( handlerRdz );
		CheckBoxHandlerRrx handlerRrx = new CheckBoxHandlerRrx();
		restraintRrx.addItemListener( handlerRrx );
		CheckBoxHandlerRry handlerRry = new CheckBoxHandlerRry();
		restraintRry.addItemListener( handlerRry );
		CheckBoxHandlerRrz handlerRrz = new CheckBoxHandlerRrz();
		restraintRrz.addItemListener( handlerRrz );
		// end of register
		
		restraintRdxPane.add(restraintRdx);
		restraintRdxPane.add(labelRdx);
		restraintRdyPane.add(restraintRdy);
		restraintRdyPane.add(labelRdy);
		restraintRdzPane.add(restraintRdz);
		restraintRdzPane.add(labelRdz);
		restraintRrxPane.add(restraintRrx);
		restraintRrxPane.add(labelRrx);
		restraintRryPane.add(restraintRry);
		restraintRryPane.add(labelRry);
		restraintRrzPane.add(restraintRrz);
		restraintRrzPane.add(labelRrz);
		
		restraintValueDPane.add(restraintRdxPane);
		restraintValueDPane.add(restraintRdyPane);
		restraintValueDPane.add(restraintRdzPane);
		restraintValueRPane.add(restraintRrxPane);
		restraintValueRPane.add(restraintRryPane);
		restraintValueRPane.add(restraintRrzPane);
		
		if (gAnlType.equals("Beam") || gAnlType.equals("PlaneFrame") || gAnlType.equals("PlaneTruss") ) {
			restraintRdz.setEnabled(false);
			restraintRrx.setEnabled(false);
			restraintRry.setEnabled(false);
		}
		else if (gAnlType.equals("Grid")) {
			restraintRdx.setEnabled(false);
			restraintRdz.setEnabled(false);
			restraintRry.setEnabled(false);
		}
		
		if ( restraintRdx.isSelected() ) {
			labelRdx.setText("Dx = 0");
			figuraRdx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDx64.gif")) );
		}
		if ( restraintRdy.isSelected() ) {
			labelRdy.setText("Dy = 0");
			figuraRdy.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDy64.gif")) );
		}
		if ( restraintRdz.isSelected() ) {
			labelRdz.setText("Dz = 0");
			figuraRdz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDz64.gif")) );
		}
		if ( restraintRrx.isSelected() ) {
			labelRrx.setText("Rx = 0");
			figuraRrx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRx64.gif")) );
		}
		if ( restraintRry.isSelected() ) {
			labelRry.setText("Ry = 0");
			figuraRry.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRy64.gif")) );
		}
		if ( restraintRrz.isSelected() ) {
			labelRrz.setText("Rz = 0");
			figuraRrz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRz64.gif")) );
		}
		
		setPane(restraintPane);
	}
	
//*****************************************************************************
	
    public int showDialog() {
    	return super.showDialog(this.parent, parent.getResourceBundle().getString("dNResDialog01"));
    }
	
//*****************************************************************************
	
	/**
	 * This method is executed on OK button click.
	 */
	public void approvedPerform() {
		try {
			Rdx = restraintRdx.isSelected();
			Rdy = restraintRdy.isSelected();
			Rdz = restraintRdz.isSelected();
			Rrx = restraintRrx.isSelected();
			Rry = restraintRry.isSelected();
			Rrz = restraintRrz.isSelected();
		} catch (Exception e) {
			Toolkit.getDefaultToolkit().beep();
			this.setVisible(false);
		}
	}
	
//*****************************************************************************
	/**
	 * Sets up the number formater
	 */
	private void setUpFormat() {
		numberFormat = (DecimalFormat)NumberFormat.getNumberInstance(Locale.UK);
		numberFormat.setMinimumFractionDigits(parent.getModelState().getPrecision());
		numberFormat.setMaximumFractionDigits(8);
	}
	
//*****************************************************************************
	
	/**
	 * Sets up fields
	 */
	private void setUpFields() {
		if (this.parent.getController().getSelection().size() == 1 ) {
			Node node = (Node)this.parent.getController().getSelection().get(0);
			
			restraintRdx.setSelected(node.getRestraint(0));
			restraintRdy.setSelected(node.getRestraint(1));
			restraintRdz.setSelected(node.getRestraint(2));
			restraintRrx.setSelected(node.getRestraint(3));
			restraintRry.setSelected(node.getRestraint(4));
			restraintRrz.setSelected(node.getRestraint(5));
			
		}
		
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRdx implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRdx.isSelected() ) {
				labelRdx.setText("Dx = 0");
				figuraRdx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDx64.gif")) );
			} else {
				labelRdx.setText("Dx       ");
				figuraRdx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDx64.gif")) );
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRdy implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRdy.isSelected() ) {
				labelRdy.setText("Dy = 0");
				figuraRdy.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDy64.gif")) );
			} else {
				labelRdy.setText("Dy       ");
				figuraRdy.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDy64.gif")) );
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRdz implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRdz.isSelected() ) {
				labelRdz.setText("Dz = 0");
				figuraRdz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintDz64.gif")) );
			} else {
				labelRdz.setText("Dz       ");
				figuraRdz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceDz64.gif")) );
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRrx implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRrx.isSelected() ) {
				labelRrx.setText("Rx = 0");
				figuraRrx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRx64.gif")) );
			} else {
				labelRrx.setText("Rx       ");
				figuraRrx.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRx64.gif")) );
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRry implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRry.isSelected() ) {
				labelRry.setText("Ry = 0");
				figuraRry.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRy64.gif")) );
			} else {
				labelRry.setText("Ry       ");
				figuraRry.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRy64.gif")) );
			}
		}
	}
	
//*****************************************************************************
	
	/** Private inner class to handle check box events. */
	private class CheckBoxHandlerRrz implements ItemListener {
		public void itemStateChanged( ItemEvent event ) {
			if ( restraintRrz.isSelected() ) {
				labelRrz.setText("Rz = 0");
				figuraRrz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/RestraintRz64.gif")) );
			} else {
				labelRrz.setText("Rz       ");
				figuraRrz.setIcon( new ImageIcon(ClassLoader.getSystemResource("br/ufmg/dees/insane/images/DisplaceRz64.gif")) );
			}
		}
	}
	
//*****************************************************************************
}
